/***UIBase By GYLite 2022-12-26
 * 定义了普通UI的基类
*/
class UIBase extends GYLite.GYUIComponent{
	protected _data:any;
	protected _mode:number;
	protected _moduleScale: number;
	protected _designWidth: number;
	protected _designHeight: number;
	protected _designScale:number;
	protected _designMaxRatio:number;
	protected _designMinRatio:number;
	protected _scaleMode: string;
	public constructor() {
		super();
		let s = this;
		s._designHeight = s._designWidth = NaN;
		s._scaleMode = null
		s._mode = LayerManager.MODE_COMMON;
	}
	/**重新适配模块
	 * @param w 模块的设计尺寸 宽度
	 * @param h 模块的设计尺寸 高度
	 * @param screenMode 适配方案参考LayerManager常量
	 * @param maxRatio 最宽的比例限制 默认NaN 无限制
	 * @param minRatio 最窄尺寸限制 默认0.75 4比3限制
	 * @param scale 设计的缩放比例，方便适配不同大小设备，前提是内容需要做成相对布局，否则会定位超出屏幕范围
	*/
	public resetSize(w: number, h: number, screenMode: string, maxRatio: number = NaN, minRatio: number = 0.75,scale:number=1): void {
		let s = this;		
		s._designMaxRatio = maxRatio;
		s._designMinRatio = minRatio;
		s._designScale = scale;
		s._designWidth = w;
		s._designHeight = h;
		w *= scale;
		h *= scale;
		s.width = w;
		s.height = h;
		let sclX: number, sclY: number, scl: number;
		let game: GameManager = GameManager.getInstance();
		let stdW: number, stdH: number;
		s._scaleMode = screenMode;
		scl = w / h;
		stdW = game.stardardWidth;
		stdH = game.stardardHeight;
		if (scl > maxRatio)
			stdW = h * maxRatio;
		else if (scl < minRatio)
			stdH = w / minRatio;

		sclX = stdW / w;
		sclY = stdH / h;

		if (s._scaleMode == LayerManager.FIX_WIDTH)
			s._moduleScale = sclX;
		else if (s._scaleMode == LayerManager.FIX_HEIGHT)
			s._moduleScale = sclY;
		else if (s._scaleMode == LayerManager.NOBORD)
			s._moduleScale = sclX > sclY ? sclX : sclY;
		else if (s._scaleMode == LayerManager.SHOWALL)
			s._moduleScale = sclX < sclY ? sclX : sclY;
		else
			s._moduleScale = 1;		
		s.scaleX = s.scaleY = s._moduleScale;
		s.x = game.stardardWidth - w * s._moduleScale >> 1;
		s.y = game.stardardHeight - h * s._moduleScale >> 1;
	}
	public set mode(val:number)
	{let s =this;
		s._mode = val;
		if(s._mode == LayerManager.MODE_INSCREEN && s.parent != LayerManager.getInstance().stageSp)
			LayerManager.getInstance().stageSp.addElement(s);
	}
	public get designWidth():number
	{
		return this._designWidth;
	}
	public get designHeight():number
	{
		return this._designHeight;
	}
	/**设置呈现的模式，参考LayerManager常量*/
	public get mode():number
	{
		return this._mode;
	}
	public show(pr:GYLite.GYSprite=null):void
	{let s = this;
		if(pr == null)
			pr = s._mode == LayerManager.MODE_INSCREEN?LayerManager.getInstance().stageSp:LayerManager.getInstance().uiLay;
		if(s.parent == null)
			GYLite.GYSprite.stage.addEventListener(egret.Event.RESIZE,s.resizeCall,s);
		if(pr != s.parent)
		{
			pr.addElement(s);			
		}		
	}

	public hide():void
	{let s = this;
		if(s.parent)
		{
            if((<any>s.parent).removeElement!=null)
                (<any>s.parent).removeElement(s);
            else
                (<any>s.parent).removeChild(s);
			GYLite.GYSprite.stage.removeEventListener(egret.Event.RESIZE,s.resizeCall,s);
		}			
	}
	protected resizeCall(e:egret.Event=null):void
	{let s = this;
		let layer:LayerManager = LayerManager.getInstance();
		if(s._mode == LayerManager.MODE_INSCREEN)
			s.resize(GameManager.getInstance().stageWidth,GameManager.getInstance().stageHeight);
		else
			s.resize(layer.winWidth,layer.winHeight);
	}
	public resize(w:number, h:number):void
	{
		let s = this;
		if (s._scaleMode != null)
			s.resetSize(s._designWidth, s._designHeight, s._scaleMode, s._designMaxRatio, s._designMinRatio, s._designScale);
	}

	public setData(d:any):void
	{
		let s = this;
		s._data = d;
	}
	public getData():any
	{
		return this._data;
	}
}